package org.gosdt.ew.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.gosdt.ew.model.Languages;
import org.gosdt.ew.model.Profiles;

public class SettingsForm extends BaseForm {

	/**
	 * Form for doing program settings such as: Profile Setup
	 */
	private static final long serialVersionUID = 1L;

	JPanel jpnAvailableActions = new JPanel(); // any common buttons
	JPanel jpnAvailableSettings = new JPanel(null); // all other thing

	JButton jbtSaveButton = new JButton();
	JButton jbtCloseButton = new JButton();

	JButton jbtAddNewLanguage = new JButton();
	JButton jbtAddNewLanguage2 = new JButton();

	// settings fields
	JComboBox jcbProfile = new JComboBox();
	JTextField jtfTimeaInterval = new JTextField();
	JTextField jtfAskCount = new JTextField();
	JComboBox jcbNativeLanguage = new JComboBox();
	JComboBox jcbStudyLanguage = new JComboBox();
	// settings field labels
	JLabel jlbProfile = new JLabel();
	JLabel jlbTimeInterval = new JLabel();
	JLabel jlbAskCount = new JLabel();
	JLabel jlbNativeLanguage = new JLabel();
	JLabel jlbStudyLanguage = new JLabel();

	public SettingsForm(JFrame parent) {
		super(parent, "change settings");
		doInitForm();
		this.setVisible(true);
	}

	@Override
	public void doInitForm() {
		jcbProfile.setEditable(true);
		this.setLayout(new BorderLayout());

		setLabelsName();
		int fWidth = myParent.getWidth();
		int large = (int) (fWidth * 0.7);
		int smal = (int) (fWidth * 0.27);
		int magic = 210; // pixel for good button

		if (fWidth <= magic) {
			jbtCloseButton.setPreferredSize(new Dimension(
					(int) (fWidth * 0.47), 25));
			jbtSaveButton.setPreferredSize(new Dimension((int) (fWidth * 0.47),
					25));
			jpnAvailableActions.setLayout(new FlowLayout(FlowLayout.CENTER,
					(int) (fWidth * 0.02), 0));
		}
		// add actions components
		jpnAvailableActions.add(jbtCloseButton);
		jpnAvailableActions.add(jbtSaveButton);
		// settings setup
		jlbProfile.setBounds(5, 5, large, 25);
		jcbProfile.setBounds(large + 5, 5, smal, 25);
		jlbNativeLanguage.setBounds(5, 35, large - 35, 25);
		jcbNativeLanguage.setBounds(large - 30, 35, smal, 25);
		jbtAddNewLanguage.setBounds(large - 25 + smal, 35, 25, 25);
		jlbStudyLanguage.setBounds(5, 65, large - 35, 25);
		jcbStudyLanguage.setBounds(large - 30, 65, smal, 25);
		jbtAddNewLanguage2.setBounds(large - 25 + smal, 65, 25, 25);
		jlbTimeInterval.setBounds(5, 95, large, 25);
		jtfTimeaInterval.setBounds(large + 5, 95, smal, 25);
		jlbAskCount.setBounds(5, 125, large, 25);
		jtfAskCount.setBounds(large + 5, 125, smal, 25);
		// add components
		jpnAvailableSettings.add(jlbProfile);
		jpnAvailableSettings.add(jcbProfile);
		jpnAvailableSettings.add(jlbNativeLanguage);
		jpnAvailableSettings.add(jcbNativeLanguage);
		jpnAvailableSettings.add(jbtAddNewLanguage);
		jpnAvailableSettings.add(jlbStudyLanguage);
		jpnAvailableSettings.add(jcbStudyLanguage);
		jpnAvailableSettings.add(jbtAddNewLanguage2);
		jpnAvailableSettings.add(jlbTimeInterval);
		jpnAvailableSettings.add(jtfTimeaInterval);
		jpnAvailableSettings.add(jlbAskCount);
		jpnAvailableSettings.add(jtfAskCount);

		this.add(jpnAvailableActions, BorderLayout.SOUTH);
		this.add(jpnAvailableSettings, BorderLayout.CENTER);
		setButtonListeners();
	}

	@Override
	public void setButtonListeners() {
		ActionListener addLanguage = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				showNewLanguageForm();
			}
		};

		jbtAddNewLanguage.addActionListener(addLanguage);
		jbtAddNewLanguage2.addActionListener(addLanguage);

		jbtCloseButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				doCloseForm();
			}
		});

		jcbProfile.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Profiles profile = new Profiles(jcbProfile.getSelectedItem()
						.toString());
				profile = profile.getProfileByName(profile.getName());
				jcbNativeLanguage.setSelectedItem(profile.getNativeLanguage());
				jcbStudyLanguage.setSelectedItem(profile.getStudyLanguage());
				jtfAskCount.setText(((Integer) profile.getAskCount())
						.toString());
				jtfTimeaInterval.setText(((Integer) profile.getRestTime())
						.toString());

			}
		});

		jbtSaveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Profiles profile = new Profiles(getSelectedProfile());
				profile.setNativeLanguage(getNativeLanguage());
				profile.setStudyLanguage(getStudyLanguage());
				profile.setRestTime(getRestTime());
				profile.setAskCount(getAskCount());
				profile.setLoadByDefault(true);
				profile.insertCurRecord();
				profile.setAsDefaultProfile();
				myParent.setProfile(profile);
			}

			private Languages getStudyLanguage() {
				return Languages.GetOneRowByName(jcbStudyLanguage
						.getSelectedItem().toString());
			}

			private Languages getNativeLanguage() {
				return Languages.GetOneRowByName(jcbNativeLanguage
						.getSelectedItem().toString());
			}

			private int getAskCount() {
				return Integer.parseInt(jtfAskCount.getText());
			}

			private int getRestTime() {
				return Integer.parseInt(jtfTimeaInterval.getText());
			}

			private String getSelectedProfile() {
				return jcbProfile.getSelectedItem().toString();
			}
		});

	}

	private void showNewLanguageForm() {
		new LanguagesForm(myParent, this);
	}

	private void doCloseForm() {
		myParent.changeTimerStatus();
		myParent.updateVisualCounts();
		jbtAddNewLanguage.removeActionListener(jbtAddNewLanguage
				.getActionListeners()[0]);
		jbtAddNewLanguage2.removeActionListener(jbtAddNewLanguage2
				.getActionListeners()[0]);
		jbtCloseButton
				.removeActionListener(jbtCloseButton.getActionListeners()[0]);
		jbtSaveButton
				.removeActionListener(jbtSaveButton.getActionListeners()[0]);
		this.dispose();
	}

	@Override
	public void setLabelsName() {
		// action button setup
		jbtSaveButton.setText("Сохранить");
		jbtCloseButton.setText("Закрыть");
		// forms components setup
		jlbProfile.setText("Выбирете профиль");
		jlbTimeInterval.setText("секун между занятиями");
		jlbAskCount.setText("Количество вопросов в упражнении");
		jlbNativeLanguage.setText("Выберите родной язык");
		jlbStudyLanguage.setText("Выберите изучаемый язык");
		setLanguageItems();

		jbtAddNewLanguage.setIcon(new ImageIcon(this.getClass().getResource(
				"/org/gosdt/ew/resurse/add.gif")));
		jbtAddNewLanguage2.setIcon(new ImageIcon(this.getClass().getResource(
				"/org/gosdt/ew/resurse/add.gif")));
	}

	public void setLanguageItems() {
		jcbNativeLanguage.removeAllItems();
		Languages myI = new Languages();
		ArrayList<Languages> data = myI.getAllLanguages();
		for (int i = 0; i < data.size(); i++) {
			jcbNativeLanguage.addItem(data.get(i).getName());
			jcbStudyLanguage.addItem(data.get(i).getName());
		}

		Profiles profile = new Profiles("");
		ArrayList<Profiles> profiles = profile.getAllProfilesInDB();
		for (int i = 0; i < profiles.size(); i++) {
			jcbProfile.addItem(profiles.get(i).getName());
		}
		try {
			jcbProfile.setSelectedItem(myParent.getProfile().getName());
			jcbNativeLanguage.setSelectedItem(myParent.getProfile()
					.getNativeLanguage());
			jcbStudyLanguage.setSelectedItem(myParent.getProfile()
					.getStudyLanguage());
			jtfAskCount.setText(((Integer) myParent.getProfile().getAskCount())
					.toString());
			jtfTimeaInterval.setText(((Integer) myParent.getProfile()
					.getRestTime()).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		;

	}

}
